from copy import copy
from XML.XMLHandler import XMLHandler
from TileSeeder import TileSeeder
from TileSeed import TileSeed
from BruTile.Extent import Extent
from logging import threading

class LayerHandler(object):

    __tile_seeds = []
    inProcess = False
    tile_seeder = None
    queue = None
    __lock = None
    
    def __init__(self):
        self.__lock = threading.Lock()
        self.tile_seeder = TileSeeder()
        self.queue = self.tile_seeder.GetQue()
    
    def AreTileSeedsActive(self):
        for seed in self.__tile_seeds:
            if seed.active:
                return True
        if (self.tile_seeder.isAlive()):
            return True
        return False
    
    def IsTileSeedActive(self, name):
        tile_seed = self.GetTileSeed(name)
        if not(tile_seed is None):
            return tile_seed.active
        else:
            return False

    def ProccessTileSeed(self, seed):
        tile_seed = self.GetTileSeed(copy(seed.tile_schema.name))
        tile_seed.active = True
        self.__lock.acquire()
        try:
            self.queue.put([copy(tile_seed), False, tile_seed.tile_schema.name, False])
        finally:
            self.__lock.release()
    
    def ShowTileSeed(self, name):
        tile_seed = self.GetTileSeed(name)
        self.__lock.acquire()
        try:
            self.queue.put([copy(tile_seed), False, tile_seed.tile_schema.name, False])
        finally:
            self.__lock.release()
    
    def QuitAllTileSeeds(self):
        for tile_seed in self.__tile_seeds:
            if (tile_seed.active):
                self.__lock.acquire()
                try:
                    self.queue.put([copy(tile_seed), True, tile_seed.tile_schema.name, False])
                finally:
                    self.__lock.release()
                    self.ResetSeed(tile_seed)
        
        self.queue.put([copy(tile_seed), True, tile_seed.tile_schema.name, True])
    
    def QuitTileSeed(self, seed):
        tile_seed = self.GetTileSeed(seed.tile_schema.name)
        self.__lock.acquire()
        try:
            self.queue.put([copy(tile_seed), True, tile_seed.tile_schema.name, False])
        finally:
            self.__lock.release()
       
    def StartProcessing(self):
        self.tile_seeder.in_process = True
        self.inProcess = True
        self.tile_seeder.start()
    
    def GetTileSeedInfo(self, url, tile_seed, do_after):
        xmlHandler = XMLHandler()
        url = url + "/"
        url = str(url).replace(' ', '%20')
        xmlHandler.load_layer(url, tile_seed)
        do_after(tile_seed)
    
    def ResetSeed(self, seed):
        seedCopy = seed.seed_schema.GetCopy()
        schemaCopy = seed.tile_schema.GetCopy()
        
        self.RemoveTileSeed(seed.tile_schema.name)
        #att = seed.GetAttributes()
        #seedCopy = seed.seed_schema.GetCopy()
        
        #seed.tile_schema.active = False
        tile_seed = TileSeed()
        tile_seed.active = False
        tile_seed.SetAttributes(schemaCopy, seedCopy)
        self.AddTileSeed(tile_seed)
    '''
    def ResetSeed(self, seed):
        schemaCopy = seed.tile_schema.GetCopy()
        seedCopy = seed.seed_schema.GetCopy()
        seed.tile_schema.active = False
        tile_seed = TileSeed()
        tile_seed.active = False
        tile_seed.SetAttributes([schemaCopy, seedCopy])
        self.RemoveTileSeed(seed.tile_schema.name)
        self.AddTileSeed(tile_seed)
    '''
    
    def InitTileSeeds(self, url, list, openPscreen):
        xmlHandler = XMLHandler()
        xmlHandler.load_capabilities(url)
        list.Clear()
        has_items = False
        for attributes in xmlHandler.capabilities_handler.attributes:
            tile_seed = TileSeed()
            tile_seed.SetSeedInfo(attributes, url)
            list.Append(tile_seed.tile_schema.name, tile_seed)
            has_items = True
        return has_items
    
    def UpdateSeedExtent(self, layer, extent):
        if not layer.tile_schema.extent.equals(extent):
            if self.containsSeed(layer):
                for seed in self.__tile_seeds:
                    if (seed.tile_schema.name == layer.tile_schema.name):
                        seed.tile_schema.extent = Extent(extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY())

    def SetTileSeeds(self, layers):
        for layer in layers:
            tile_seed = TileSeed()
            tile_seed.tile_schema = copy(layer)
            self.__tile_seeds.Append(tile_seed)
            self.ResetSeed(tile_seed)
        #self.__tile_seeds = copy(seeds)
        ''' When restarting Seeds after property screen has been closed
            those seeds need to be reset.'''
        '''for layer in self.layers:
            self.__tile_seeds.Append(copy(layer))
            
            self.ResetSeed(seed)'''
    
    def AddTileSeed(self, seed):
        if not (self.ContainsTileSeed(seed.tile_schema.name)):
            self.__tile_seeds.append(copy(seed))

    def GetTileSeed(self, name):
        for seed in self.__tile_seeds:
            if seed.tile_schema.name == name:
                return seed        
        return None
    
    def GetTileSeedIndex(self, name):
        for i in range(0, len(self.__tile_seeds)):
            seed = self.__tile_seeds[i]
            if seed.tile_schema.name is name:
                return i
        return None
    
    def ContainsTileSeed(self, name):
        for seed in self.__tile_seeds:
            if seed.tile_schema.name is name:
                return True
        
        return False
    
    def RemoveTileSeed(self, name):
        index = self.GetTileSeedIndex(name)
        if not(index is None):
            self.__tile_seeds.pop(index)
    
    def GetLayerNames(self):
        names = []
        for seed in self.__tile_seeds:
            names.append(seed.tile_schema.name)
        
        return names